package cn.sh.library.gmwx.sparql;

import cn.sh.library.gmwx.base.Namespace;
import cn.sh.library.gmwx.base.Constant;

public enum RdfQuery {
	
	 //####################################BUILDING################################################
	RDF_QUERY_ARCHITECTURE_COUNT(Namespace.getNsPrefixString()
			+ "select (count(distinct ?uri) as ?count) " + "from <"
			+ Constant.GRAPH_BUILDING + "> " + "where{?uri a shl:PhysicalCulturalHeritage .{?uri ?p ?o} %1$s"
			+ " %2$s " + "}"),
	
	RDF_QUERY_ARCHITECTURE_IRI(Namespace.getNsPrefixString()
			+ "select DISTINCT ?uri " + "from <" + Constant.GRAPH_BUILDING
			+ "> " + "where{?uri a shl:PhysicalCulturalHeritage .{?uri ?p ?o} %1$s" + " %2$s "
			+ "} OFFSET %3$s LIMIT %4$s"),
	
	RDF_QUERY_ARCHITECTURE_SIMPLE(
			Namespace.getNsPrefixString()
					+ "select ?uri (str(?nameS) as ?nameS) (str(?nameT) as ?nameT) (str(?nameE) as ?nameE) ?address (GROUP_CONCAT(DISTINCT ?type;separator=';') AS ?type) ?des ?houseNumber ?placeUri (GROUP_CONCAT(DISTINCT ?yname;separator=';') AS ?yname)  "
					+ "from <"
					+ Constant.GRAPH_BUILDING
					+ "> "
					+ "where {?uri a shl:PhysicalCulturalHeritage; shl:address ?address;foaf:name ?nameE;foaf:name ?nameS;foaf:name ?nameT ;shl:place ?placeUri "
					+ "optional{?uri dc:type ?type} optional {?uri shl:description ?des} optional{?uri shl:houseNumber ?houseNumber} "
					+ "optional{?uri shl:name ?ynameUri {?ynameUri a shl:Name;rdfs:label ?yn;shl:nameType ?ynameType .BIND(concat(str(?ynameType), ':', ?yn) as ?yname)}}"
					+ ". filter(lang(?nameS)='chs')"
					+ ". filter(lang(?nameT)='cht')"
					+ ". filter(lang(?nameE)='en')"
//					+ ". filter (?type!='红色旅游景点' )"
					+ ". values ?uri{%1$s}}"),
	
	RDF_QUERY_ARCHITECTURE_DETAIL(Namespace.getNsPrefixString()
			+"select ?uri ?address ?nameE ?nameS ?nameT ?source ?type ?placeUri ?lat ?long ?des ?houseNumber "
			+ "from <"+Constant.GRAPH_BUILDING+"> "
			+ "where {?uri a shl:PhysicalCulturalHeritage; shl:address ?address;foaf:name ?nameE;foaf:name ?nameT "
			+ ";foaf:name ?nameS;dc:source ?source;dc:type ?type;shl:place ?placeUri;shl:location ?location "
			+ "{?location a shl:Location;geo:lat ?lat;geo:long ?long} optional{?uri shl:description ?des} "
			+ "optional{?uri shl:houseNumber ?houseNumber} "
			+ " .filter(lang(?nameE)='en') .filter(lang(?nameS)='chs') .filter(lang(?nameT)='cht') .values ?uri{<%1$s>}}"),
	
	RDF_QUERY_EVENT_SIMPLE(Namespace.getNsPrefixString()+"select  ?description ?startedAtTime ?endedAtTime "
			+ "from <"+Constant.GRAPH_BUILDING+"> "
					+ "where {<%1$s> shl:event ?event{?event a "
					+ "shl:Event;shl:description ?description "
					+ "optional{?event prov:startedAtTime ?startedAtTime} "
					+ "optional{?event prov:endedAtTime ?endedAtTime} "
					+ "} } order by ?startedAtTime"),
	
	RDF_QUERY(Namespace.getNsPrefixString()+"select * from <%1$s> where{?s ?p ?o.filter(str(?s)='%2$s')}" ),
	
    //####################################BASEINFO################################################
    RDF_QUERY_TRIPLES(Namespace.getNsPrefixString() +
            "SELECT ?s ?p ?o " +
            "WHERE {" +
            "   ?s ?p ?o ." +
            "   FILTER (?s = <%1$s>)" +
            "}"),

    RDF_QUERY_VOCAB_LIST(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?label ?label_en " +
            "WHERE {" +
            "   ?uri a %1$s ; " +
            "        rdfs:label ?label ; " +
            "        rdfs:label ?label_en . " +
            "   FILTER (lang(?label) = 'zh-cn')" +
            "   FILTER (lang(?label_en) = 'eng')" +
            "} ORDER BY ?label_en"),

    //####################################ORGANIZATION################################################
    RDF_COUNT_ORG_Q(Namespace.getNsPrefixString() +
            "SELECT (COUNT(DISTINCT ?uri) AS ?count) " +
            "WHERE {" +
            "   %1$s" +
            "}"),

    RDF_QUERY_ORG_Q(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?name ?type " +
            "WHERE {" +
            "SELECT DISTINCT ?uri (STR(?label) AS ?name) ?type " +
            "WHERE {" +
            "   ?uri skos:prefLabel ?label . " +
            "   OPTIONAL {" +
            "       ?uri skos:prefLabel ?py_name .  " +
            "       FILTER (lang(?py_name) = 'zh-pny')" +
            "   }" +
            "   OPTIONAL {?uri shl:organizationType ?type . } " +
            "   FILTER (lang(?label) = 'zh-cn')" +
            "   %1$s " +
//            "   SERVICE SILENT <%2$s> {" +
//            "       ?reg bf:label ?region . " +
//            "       FILTER (lang(?region) = 'chs')" +
//            "   } " +
            "} %2$s " +
            "} OFFSET %3$s LIMIT %4$s"),

    //####################################INSTANCE################################################
    RDF_QUERY_INSTANCE_WORKS_INFO(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri (STR(?label) AS ?title) ?creator " +
            "FROM <" + Constant.GRAPH_INSTANCE + "> " +
            "FROM <" + Constant.GRAPH_WORK + "> " +
            "WHERE {" +
            "   <%1$s> bf:instanceOf ?uri . " +
            "   OPTIONAL { ?uri dc:creator ?creator . } " +
            "   OPTIONAL { ?uri rdfs:label ?label . } " +
            "   FILTER (lang(?label) = 'zh-cn')" +
            "}"),

    RDF_QUERY_INSTANCE_CONTRIBUTIONS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?role " +
            "WHERE {" +
            "   <%1$s> bf:contribution ?cont . " +
            "   ?cont bf:agent ?uri . " +
            "   OPTIONAL{?cont  bf:role/rdfs:label ?role }. " +
            "}"),

    RDF_QUERY_REMOTE_PERSON_NAME(Namespace.getNsPrefixString() +
            "SELECT (STR(?label) AS ?name) " +
            "WHERE {" +
            "   SERVICE SILENT <%1$s> {" +
            "       <%2$s> foaf:name ?label . " +
            "       FILTER (lang(?label) = 'chs')" +
            "   }" +
            "}"),

    RDF_QUERY_INSTANCE_DETAIL(Namespace.getNsPrefixString() +
            "SELECT ?uri (STR(?title_label) AS ?title) ?dimension ?extent ?year ?edition ?terms ?hor ?agent (STR(?org) AS ?publisher) ?placeUri ?series ?ilabel " +
            "from <http://gmwx.library.sh.cn/graph/image> " +
            "from <http://gmwx.library.sh.cn/graph/instance> " +
            "WHERE {" +
            "   ?uri bf:title/rdfs:label ?title_label . " +
            "   OPTIONAL {?uri bf:dimensions ?dimension . }" +
            "   OPTIONAL {?uri bf:extent/rdfs:label ?extent . }" +
            "   OPTIONAL {?uri bf:acquisitionTerms ?terms . }" +
            "   OPTIONAL {?uri bf:provisionActivity/bf:date ?year . }" +
            "   OPTIONAL {?uri bf:provisionActivity/bf:agent ?agent . ?agent skos:prefLabel ?org . FILTER (lang(?org) = 'zh-cn')}" +
            "   OPTIONAL {?uri bf:provisionActivity/bf:place ?placeUri . }" + 
            "   OPTIONAL {?uri bf:identifiedBy ?ib. ?ib a bf:Identifier ; bf:source/bf:code 'Horizon-SHL' ; rdf:value ?hor .}" +
            "   OPTIONAL {?uri bf:hasSeries/rdfs:label ?series .}" +
            "   OPTIONAL {?uri bf:editionStatement ?edition .}" +
            "   OPTIONAL {?uri shl:image/rdfs:label ?ilabel .}" +
            "   FILTER (?uri = <%1$s>)" +
            "}"),

    RDF_QUERY_WORK_COVER_HOR(Namespace.getNsPrefixString() +
            "SELECT ?hor " +
            "WHERE {" +
            "   ?uri bf:instanceOf <%1$s> ; " +
            "        bf:identifiedBy ?ib. ?ib a bf:Identifier ; bf:source/bf:code 'Horizon-SHL' ; rdf:value ?hor . " +
            "   ?uri bf:provisionActivity/bf:date ?value . " +
            "} ORDER BY ASC(?value) LIMIT 1"),

    RDF_FACET_INSTANCE_YAS(Namespace.getNsPrefixString() +
            "SELECT (STR(?v) AS ?facet_text) (COUNT(DISTINCT ?uri) AS ?cnt) " +
            "FROM <" + Constant.GRAPH_INSTANCE + "> " +
            "FROM <" + Constant.GRAPH_ORGANIZATION + "> " +
            "FROM <" + Constant.GRAPH_ITEM + "> " + 
            "WHERE {  " +
            "   {" +
            "       SELECT ?uri (min(xsd:int(?value)) AS ?v) WHERE {" +
            "           ?uri bf:provisionActivity/bf:date ?value ." +
            "           FILTER (?value != '')" +
            "           FILTER (xsd:int(?value) <= %2$s)" +
            "           %1$s" +
            "       }" +
            "   }" +
//            "} ORDER BY DESC(?facet_text)"),
    		"} ORDER BY ASC(?facet_text)"),
    
    RDF_QUERY_YEARS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?year " +
            "FROM <" + Constant.GRAPH_TEMP + "> " +
            "WHERE {" +
            "   ?s a <http://www.w3.org/2006/time#Instant> ; " +
            "      rdfs:label ?year . " +
            "   %1$s" +
            "}"),

    RDF_FACET_INSTANCE_PLS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT (STR(?place) AS ?s) (COUNT(DISTINCT ?uri) AS ?cnt) " +
            "FROM <" + Constant.GRAPH_INSTANCE + "> " +
            "FROM <" + Constant.GRAPH_ORGANIZATION + "> " +
            "FROM <" + Constant.GRAPH_ITEM + "> " + 
            "WHERE {" +
            "   ?uri %2$s ?place . " +
            "   FILTER (!isBlank(?place))" +
            "   %1$s" +
            "} ORDER BY DESC(?cnt)"),

    RDF_FACET_INSTANCE_PUS(Namespace.getNsPrefixString() +
            "SELECT (STR(?org) AS ?s) (STR(?value) AS ?facet_text) (COUNT(DISTINCT ?uri) AS ?cnt) " +
            "FROM <" + Constant.GRAPH_INSTANCE + "> " +
            "FROM <" + Constant.GRAPH_ORGANIZATION + "> " +
            "FROM <" + Constant.GRAPH_ITEM + "> " + 
            "WHERE {" +
            "    { ?uri %2$s ?org . }" +
            "   %1$s" +
            "    { ?org skos:prefLabel ?value . " +
            "   FILTER (lang(?value) = 'zh-cn') }" +
            "} ORDER BY DESC(?cnt)"),
    
    // facetFullTextFlg
    RDF_FACET_INSTANCE_FTF(Namespace.getNsPrefixString() +
            "SELECT  ?fulltextFlg  count(distinct ?uri) as ?count " +
            "FROM <" + Constant.GRAPH_INSTANCE + "> " +
            "WHERE {" +
            " ?uri a bf:Instance; bf:title ?title. optional {?uri %2$s ?image} " +
            " bind( if(strlen(?image)>0, '0','1') as ?fulltextFlg ) " +
            " %1$s" +
            "} group by ?fulltextFlg order by ?fulltextFlg "),

    RDF_QUERY_INSTANCE_REMOTE_PLACE(Namespace.getNsPrefixString() +
            "SELECT ?label  " +
            "WHERE {" +
            "   SERVICE SILENT <%1$s> {" +
            "       <%2$s> <http://bibframe.org/vocab/label> ?label . " +
            "       FILTER (lang(?label) = 'chs')" +
            "   }" +
            "}"),

    RDF_COUNT_INSTANCE_Q(Namespace.getNsPrefixString() +
            "SELECT (COUNT(DISTINCT ?uri) AS ?count) " +
    		" FROM <" + Constant.GRAPH_INSTANCE + "> " + 
    		" FROM <" + Constant.GRAPH_ITEM + "> " + 
            "WHERE {" +
            "   ?uri a bf:Instance . " +
            "   %1$s" +
            "   FILTER (!isBlank(?uri))" +
            "}"),
    RDF_QUERY_INSTANCE_Q(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri " +
    		" FROM <" + Constant.GRAPH_INSTANCE + "> " + 
    		" FROM <" + Constant.GRAPH_ITEM + "> " + 
            "WHERE {" +
            "   ?uri a bf:Instance . " +
            "   FILTER (!isBlank(?uri))" +
//            "        rdfs:label ?tlabel ; " +
//            "        bf:provisionActivityStatement ?provision . " +
//            "   OPTIONAL {?uri bf:provisionActivity/bf:date ?year .}" +
            "   %1$s" +
//            "   optional { ?uri rdfs:label ?label } " +
//            "   optional { ?uri bf:responsibilityStatement ?creator } " +
            "   optional { ?uri shl:image ?image } " +
//            "   }  order by %2$s desc(?label) desc(?creator) " + 
            "   }  order by %2$s desc (?image) " +
            "   OFFSET %3$s LIMIT %4$s"),

    RDF_QUERY_PLACE_IN_INSTANCE(Namespace.getNsPrefixString() +
    		" SELECT DISTINCT ?placeUri as ?place ?label ?long ?lat (count(distinct ?uri) as ?cnt)" + 
    		" FROM <" + Constant.GRAPH_INSTANCE + "> " + 
    		" FROM <" + Constant.GRAPH_PLACE + "> " +
    		" FROM <" + Constant.GRAPH_GEO + "> " +
    		" WHERE{ " + 
    		" { ?placeUri owl:sameAs ?as } " +
    		" UNION {?placeUri owl:sameAs/owl:seeAlso ?as } " + 
    		" ?placeUri <http://bibframe.org/vocab/label> ?label . " + 
    		" FILTER (lang(?label)='cht') " + 
    		" ?as geo:geometry ?geo; geo:long ?long; geo:lat ?lat . " + 
    		" FILTER (bif:st_within(bif:st_point(?long, ?lat), bif:st_geomfromtext('%1$s'))) " + 
    		" { " + 
    		"  SELECT DISTINCT ?placeUri ?uri " + 
    		"  WHERE{?uri a bf:Instance ; bf:provisionActivity/bf:place ?placeUri. " + 
    		"   %2$s " + 
    		"  } " + 
    		" } " + 
    		"FILTER (!isBlank(?uri)) }"),

    RDF_QUERY_PLACE_IN_INSTANCE_COUNT(Namespace.getNsPrefixString() +
    		" SELECT (count(distinct ?uri) as ?cnt)" + 
    		" FROM <" + Constant.GRAPH_INSTANCE + "> " + 
    		" FROM <" + Constant.GRAPH_PLACE + "> " +
    		" FROM <" + Constant.GRAPH_GEO + "> " +
    		" WHERE{ " + 
    		" { ?placeUri owl:sameAs ?as } " +
    		" UNION {?placeUri owl:sameAs/owl:seeAlso ?as } " + 
    		" ?placeUri <http://bibframe.org/vocab/label> ?label . " + 
    		" FILTER (lang(?label)='cht') " + 
    		" ?as geo:geometry ?geo; geo:long ?long; geo:lat ?lat . " + 
    		" FILTER (bif:st_within(bif:st_point(?long, ?lat), bif:st_geomfromtext('%1$s'))) " + 
    		" { " + 
    		"  SELECT DISTINCT ?placeUri ?uri " + 
    		"  WHERE{?uri a bf:Instance ; bf:provisionActivity/bf:place ?placeUri. " + 
    		"   %2$s " + 
    		"  } " + 
    		" } " + 
    		"FILTER (!isBlank(?uri)) }"),
    
    RDF_QUERY_INSTANCE_INFO(Namespace.getNsPrefixString() +
            "SELECT (STR(sql:BEST_LANGMATCH(?tlabel, 'zh-CN;q=0.8, eng;q=0.7, ;q=0.6, en;q=0.5, *;q=0.1', 'zh-CN')) AS ?title) " +
            "?response ?provision ?dimensions ?page ?terms ?year ?hor " +
            "?series ?seresponse ?seuri ?ilabel ?workUri " +
            "(GROUP_CONCAT(DISTINCT ?personInfo; separator=';') AS ?personInfos) " + // add by CM
            "from <http://gmwx.library.sh.cn/graph/image> " +
            "from <http://gmwx.library.sh.cn/graph/instance>" +
            "WHERE {" +
            "   <%1$s> a bf:Instance ; rdfs:label ?tlabel . " + 
            "   <%1$s> bf:instanceOf ?workUri . " + 
            "   OPTIONAL {<%1$s> bf:provisionActivityStatement ?provision .} " +
            "   OPTIONAL {<%1$s> bf:contribution ?contribution. ?contribution bf:agent ?personUri; bf:role/rdfs:label ?role .}  " + // add by CM
            "   .BIND(CONCAT(str(?personUri), '~', str(?role)) AS ?personInfo) " + // add by CM
            "   OPTIONAL {<%1$s> bf:responsibilityStatement ?response .}" +
            "   OPTIONAL {<%1$s> bf:dimensions ?dimensions .}" +
            "   OPTIONAL {<%1$s> bf:extent/rdfs:label ?page .}" +
            "   OPTIONAL {<%1$s> bf:acquisitionTerms ?terms .}" +
            "   OPTIONAL {" +
            "       SELECT (min(xsd:int(?value)) AS ?year) WHERE {" +
            "           <%1$s> bf:provisionActivity/bf:date ?value ." +
            "       }" +
            "   }" +
            "   OPTIONAL {<%1$s> bf:identifiedBy ?ib. ?ib a shlbib:UBSN. ?ib rdf:value ?hor .}" +
            "   OPTIONAL {<%1$s> bf:hasSeries/rdfs:label ?series .}" +
            "   OPTIONAL {<%1$s> bf:hasSeries/bf:responsibilityStatement ?seresponse .}" +
            "   OPTIONAL {<%1$s> bf:hasSeries/bf:instanceOf ?seuri .}" +
            "   OPTIONAL {<%1$s> shl:image/rdfs:label ?ilabel .}" +
            "}"),

    RDF_QUERY_INSTANCE_WORKS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri " +
            "WHERE {" +
            "   <%1$s> bf:instanceOf ?uri . " +
            "}"),

    RDF_QUERY_INSTANCE_IDS(Namespace.getNsPrefixString() +
            "SELECT (GROUP_CONCAT(DISTINCT ?id; separator='; ') AS ?ids) " +
            "WHERE {" +
            "    <%1$s> bf:identifiedBy ?ib . " +
            "    ?ib a ?type ; " +
            "        rdf:value ?value . " +
            "   FILTER (?type != bf:Identifier) " +
            "   BIND (CONCAT(?value, '[', ?type, ']') AS ?id)" +
            "}"),

    RDF_QUERY_INSTANCE_SHELFS(Namespace.getNsPrefixString() +
            "SELECT (GROUP_CONCAT(DISTINCT ?shelf; separator='; ') AS ?shelfs) " +
            "FROM <" + Constant.GRAPH_ITEM + "> " +
            "WHERE {" +
            "    ?s bf:shelfMark/rdfs:label ?shelf ; bf:itemOf <%1$s>." +
            "} GROUP BY ?s"),

    RDF_QUERY_INSTANCE_NOTES(Namespace.getNsPrefixString() +
            "SELECT (GROUP_CONCAT(DISTINCT ?note; separator='; ') AS ?notes) " +
            "WHERE {" +
            "    <%1$s> bf:note ?in . " +
            "    ?in rdfs:label ?label . " +
            "    OPTIONAL {?in bf:noteType ?nt .}" +
            "    BIND (CONCAT(?label, '[', ?nt, ']') AS ?not)" +
//            "    BIND (CONCAT(str(?label), '[', ?nt, ']') AS ?not)" +
            "    BIND (REPLACE(?not, '\\\\[]', '') as ?note)" +
            "}"),

    RDF_QUERY_WORK_INSTANCES(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri (STR(sql:BEST_LANGMATCH(?tlabel, 'zh-CN;q=0.8, eng;q=0.7, ;q=0.6, en;q=0.5, *;q=0.1', 'zh-CN')) AS ?title) " +
            "?response ?provision ?dimensions ?page ?terms ?year ?shelf " +
            "?series ?seresponse ?seuri " +
            "WHERE {" +
            "   ?uri bf:hasSeries/bf:instanceOf|bf:instanceOf <%1$s> ; " +
            "        rdfs:label ?tlabel ; " +
            "        bf:responsibilityStatement ?response ; " +
            "        bf:provisionActivityStatement ?provision . " +
            "   OPTIONAL {?uri bf:dimensions ?dimensions .}" +
            "   OPTIONAL {?uri bf:extent/rdfs:label ?page .}" +
            "   OPTIONAL {?uri bf:acquisitionTerms ?terms .}" +
            "   OPTIONAL {?uri bf:provisionActivity/bf:date ?year .}" +
            "   OPTIONAL {?uri bf:hasSeries/rdfs:label ?series .}" +
            "   OPTIONAL {?uri bf:hasSeries/bf:responsibilityStatement ?seresponse .}" +
            "   OPTIONAL {?uri bf:hasSeries/bf:instanceOf ?seuri .}" +
            "   OPTIONAL {?s bf:shelfMark/rdfs:label ?shelf ; bf:itemOf ?uri.}" +
            "} ORDER BY DESC(?year)"),

    //####################################ITEM################################################
    RDF_QUERY_INSTANCE_ITEMS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?shelf ?org ?identifier ?pdfFlag " +
            "WHERE {" +
            "   ?uri bf:itemOf <%1$s> ; " +
            "   bf:shelfMark/rdfs:label ?shelf ; " +
            "   bf:shelfMark/bf:source ?org . " +
            "   OPTIONAL{?uri dc:identifier ?identifier .} " +
			"   OPTIONAL{?uri dc:pdfFlag ?pdfFlag .} " +
            "}"),
    RDF_QUERY_INSTANCE_ITEMS_IDENTIFIER(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?identifier " +
            "WHERE {" +
            "   ?uri bf:itemOf <%1$s> ; " +
            "   dc:identifier ?identifier  " +
            "} LIMIT 1"),

    //####################################TEMP################################################
    RDF_UPDATE_WORK_FACET(Namespace.getNsPrefixString() +
            "INSERT {" +
            "   GRAPH <" + Constant.GRAPH_TEMP + "> {" +
            "       ?uri a ?type ; rdfs:label ?facet ; rdfs:comment ?cnt ." +
            "   }" +
            "}" +
            "WHERE {" +
            "   {SELECT ?uri (STR(?label) AS ?facet) (COUNT(DISTINCT ?s) AS ?cnt) ?type " +
            "    WHERE {" +
            "       GRAPH <" + Constant.GRAPH_UNIFY + "> {" +
            "           ?s %1$s ?uri ." +
            "           ?uri rdfs:label ?label ; a ?type. " +
            "           FILTER (lang(?label) = 'zh-cn')" +
            "       }" +
            "    } ORDER BY DESC(?cnt)" +
            "   }" +
            "}"),

    RDF_QUERY_FACET_YEAR(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?facet_text ?cnt " +
            "WHERE {" +
            "   ?uri a %1$s ; " +
            "        rdfs:label ?facet_text ; " +
            "        rdfs:comment ?cnt . " +
            "   FILTER (xsd:int(?facet_text) <= %2$s)" +
//            "} ORDER BY DESC(xsd:int(?facet_text))"),
    		"} ORDER BY ASC(xsd:int(?facet_text))"),

    RDF_QUERY_WORK_FACET(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri (STR(?uri) AS ?s) ?facet_text ?cnt " +
            "WHERE {" +
            "   ?uri a %1$s ; " +
            "        rdfs:label ?facet_text ; " +
            "        rdfs:comment ?cnt . " +
            "} ORDER BY DESC(xsd:int(?cnt))"),
    //FULLTEXTFLG
    RDF_QUERY_FULLTEXT_FACET(Namespace.getNsPrefixString() +
    		"SELECT  ?fulltextFlg  count(distinct ?uri) as ?count " +
            "FROM <" + Constant.GRAPH_INSTANCE + "> " +
            "WHERE {" +
            " ?uri a bf:Instance; bf:title ?title. optional {?uri %1$s ?image} " +
            " bind( if(strlen(?image)>0, '0','1') as ?fulltextFlg ) " +
            "} group by ?fulltextFlg order by ?fulltextFlg "),
    //####################################UNIFY################################################
    //publish
    RDF_COUNT_INSTANCE_FACET(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri (COUNT(DISTINCT ?s) AS ?count) (STR(?label) AS ?facet) ?type " +
            "WHERE {" +
            "   ?s %1$s ?uri . " +
            "   ?uri skos:prefLabel ?label ; " +
            "        a ?type . " +
            "   FILTER (lang(?label) = 'zh-cn')" +
            "}"),

    //place
    RDF_COUNT_INSTANCE_FACET_PLACE(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri (COUNT(DISTINCT ?s) AS ?count) " +
            "WHERE {" +
            "   ?s %1$s ?uri . " +
            "   FILTER (!isBlank(?uri))" +
            "}"),

    //year
    RDF_COUNT_INSTANCE_FACET_YEAR(Namespace.getNsPrefixString() +
            "SELECT (STR(?v) AS ?facet) (COUNT(DISTINCT ?uri) AS ?count) " +
            "WHERE {  " +
            "   {" +
            "       SELECT ?uri (min(xsd:int(?value)) AS ?v) WHERE {" +
            "           ?uri %1$s ?value ." +
            "       }" +
            "   }" +
            "}"),

    RDF_COUNT_WORK_FACET(Namespace.getNsPrefixString() +
//            "SELECT DISTINCT ?uri (COUNT(DISTINCT ?s) AS ?count) (STR(?label) AS ?facet) ?type " +
//            "WHERE {" +
//            "   ?s %1$s ?uri . " +
//            "   ?uri rdfs:label ?label ; " +
//            "        a ?type . " +
//            "   FILTER (lang(?label) = 'zh-cn')" +
//            "   FILTER (?type != skos:Concept)" +
//            "}" +
            "SELECT ?uri ?count (STR(?label) AS ?facet) " +
            "FROM <" + Constant.GRAPH_BASEINFO + "> " +
            "WHERE {" +
            "   ?uri rdfs:label ?label . FILTER (lang(?label) = 'zh-cn')" +
            "   {SELECT DISTINCT ?uri (COUNT(DISTINCT ?s) AS ?count)" +
            "    WHERE {" +
            "       ?s %1$s ?uri ." +
            "   }}" +
            "}"),

    RDF_COUNT_WORK_FACET_LANGUAGE(Namespace.getNsPrefixString() +
            "SELECT ?uri ?count (STR(?label) AS ?facet) " +
            "FROM <" + Constant.GRAPH_BASEINFO + "> " +
            "WHERE {" +
            "   ?uri rdfs:label ?label . FILTER (lang(?label) = 'zh-cn')" +
            "   {SELECT DISTINCT ?uri (COUNT(DISTINCT ?s) AS ?count) " +
            "    WHERE {" +
            "       ?s bf:language ?lang ." +
            "       ?lang bf:identifiedBy ?uri ; bf:part '正文语种'. " +
            "   }}" +
            "}"),

    RDF_CONSTRUCT_TARGET_TRIPLES(Namespace.getNsPrefixString() +
            "CONSTRUCT {" +
            "   ?s ?p ?o . " +
            "   <%1$s> ?p0 ?s . " +
            "   <%1$s> ?p1 ?s1 . ?s1 ?p2 ?s ." +
            "} FROM <" + Constant.GRAPH_UNIFY + "> " +
            "WHERE {" +
            "   {" +
            "       ?s ?p ?o ." +
            "       FILTER (!isBlank(?o)) " +
            "       FILTER (?s = <%1$s>)" +
            "   } UNION {" +
            "       <%1$s> ?p0 ?s . ?s ?p ?o ." +
            "       FILTER ((isBlank(?s)) && (!isBlank(?o)))" +
            "   } UNION {" +
            "       <%1$s> ?p1 ?s1 . ?s1 ?p2 ?s . ?s ?p ?o." +
            "       FILTER ((isBlank(?s1)) && (isBlank(?s)))" +
            "   }" +
            "}"),

    RDF_COUNT_WORK_REMOTE_PERSONS(Namespace.getNsPrefixString() +
            "SELECT (COUNT(DISTINCT ?uri) AS ?count) " +
            "WHERE {" +
            "   ?s bf:contribution/bf:agent ?uri . " +
            "   SERVICE SILENT <%1$s> {" +
            "       ?uri foaf:name|shl:courtesyName ?label . " +
            "       FILTER (STR(?label) = '%2$s')" +
            "   }" +
            "}"),

    RDF_QUERY_WORK_REMOTE_PERSONS_1(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?name ?birthday ?deathday ?brief " +
            "WHERE {" +
            "   ?s bf:contribution/bf:agent ?uri . " +
            "   ?uri foaf:name ?name . " +
            "   OPTIONAL {?uri shl:birthday ?birthday . }" +
            "   OPTIONAL {?uri shl:deathday ?deathday . }" +
            "   OPTIONAL {?uri shl:briefBiography ?brief . }" +
            "} ORDER BY ?birthday"),

    RDF_QUERY_WORK_REMOTE_PERSONS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?name ?birthday ?deathday " +
//            "(GROUP_CONCAT(?briefBio; separator='; ') AS ?brief) " +
            "WHERE {" +
//            "   ?s bf:contribution/bf:agent ?uri . " +
            "   SERVICE SILENT <%1$s> {" +
            "       {" +
            "           ?uri foaf:name '%2$s'@chs ; foaf:name ?name . filter (lang(?name) = 'chs') " +
            "           OPTIONAL {?uri shl:birthday ?birthday . }" +
            "           OPTIONAL {?uri shl:deathday ?deathday . }" +
            "       } UNION {" +
            "           ?uri shl:name ?ns ; foaf:name ?name . filter (lang(?name) = 'chs') " +
            "           ?ns rdfs:label ?label . " +
//            "           filter (bif:contains(?label, '\"%2$s\"'))" +
            "           filter (<bif:contains>(?label, '\"%2$s\"'))" +
            "           filter (str(?label) = '%2$s')" +
            "           OPTIONAL {?uri shl:birthday ?birthday . }" +
            "           OPTIONAL {?uri shl:deathday ?deathday . }" +
//            "           OPTIONAL {?uri shl:briefBiography ?brief . filter (bif:contains(?brief, '国图规范档'))}" +
            "       }" +
            "   }" +
            "} ORDER BY ?birthday"),
//            "} %3$s OFFSET %4$s LIMIT %5$s"),

    RDF_QUERY_WORK_REMOTE_PERSON_BRIEF(Namespace.getNsPrefixString() +
            "SELECT ?brief " +
            "WHERE {" +
            "   SERVICE SILENT <%1$s> {" +
            "       <%2$s> shl:briefBiography ?brief . " +
//            "       filter (bif:contains(?brief, '\"国图规范档\"'))" +
            "       filter (<bif:contains>(?brief, '\"国图规范档\"'))" +
            "   }" +
            "}"),

    RDF_CONSTRUCT_WORK_REMOTE_PERSONS(Namespace.getNsPrefixString() +
            "CONSTRUCT {" +
            "   ?uri foaf:name ?name ; " +
            "        shl:birthday ?birthday ; " +
            "        shl:deathday ?deathday ; " +
            "        shl:briefBiography ?brief . " +
            "}" +
//            "SELECT DISTINCT ?uri ?name ?birthday ?deathday ?brief " +
            "WHERE {" +
//            "   ?s bf:contribution/bf:agent ?uri . " +
            "   SERVICE <%1$s> {" +
            "       ?uri foaf:name ?name ; " +
            "            foaf:name|shl:courtesyName ?label . " +
            "       OPTIONAL {?uri shl:birthday ?birthday . }" +
            "       OPTIONAL {?uri shl:deathday ?deathday . }" +
            "       OPTIONAL {?uri shl:briefBiography ?brief . }" +
            "       FILTER (lang(?name) = 'chs')" +
            "       FILTER (STR(?label) = '%2$s')" +
            "   }" +
            "}"),

    RDF_COUNT_WORK_FREETEXT(Namespace.getNsPrefixString() +
            "SELECT (COUNT(DISTINCT ?uri) AS ?count) " +
            "WHERE {" +
            "   %1$s" +
            "}"),

    RDF_QUERY_WORK_FREETEXT(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri ?title ?note " +
            "WHERE {" +
            "   ?uri a bf:Work ; " +
            "        bf:title/rdfs:label ?title . " +
            "   OPTIONAL {?uri bf:summary/rdfs:label ?note .}" +
            "   %1$s" +
            "} %2$s OFFSET %3$s LIMIT %4$s"),

    RDF_QUERY_WORK_DETAIL(Namespace.getNsPrefixString() +
            "SELECT (STR(?label) AS ?title) ?creator ?note ?subject ?transuri (STR(?tranlabel) AS ?translation) ?language ?classification ?contributions " +
            "WHERE {" +
            "   <%1$s> bf:title/rdfs:label ?label . " +
            "   OPTIONAL {<%1$s> dc:creator ?creator . }" +
            "   OPTIONAL {<%1$s> bf:summary/rdfs:label ?note .}" +
            "   OPTIONAL {" +
            "       SELECT (GROUP_CONCAT(DISTINCT ?sub; separator='; ') AS ?subject) " +
            "       WHERE {" +
            "           <%1$s> bf:subject/rdfs:label ?sub ." +
            "       }" +
            "   }" +
            "   OPTIONAL {" +
            "       SELECT (STR(?lanlabel) AS ?language) " +
            "       WHERE {" +
            "           <%1$s> bf:language ?lang . " +
            "           ?lang bf:identifiedBy/rdfs:label ?lanlabel ; " +
            "                 bf:part '正文语种' . " +
            "           FILTER (lang(?lanlabel) = 'zh-cn')" +
            "       }" +
            "   }" +
            "   OPTIONAL {<%1$s> bf:translationOf ?transuri ; bf:translationOf/bf:title/rdfs:label ?tranlabel .}" +
            "   OPTIONAL {<%1$s> bf:classification/bf:classificationPortion ?classification .}" +
            "   OPTIONAL {" +
            "       SELECT (GROUP_CONCAT(?contribution; separator='; ') AS ?contributions) " +
            "       WHERE {" +
            "           <%1$s> bf:contribution ?agent . " +
            "           ?agent bf:agent ?person ; " +
            "                  bf:role ?role . " +
            "           BIND (CONCAT(?person, '[', ?role, ']') AS ?contribution)" +
            "       }" +
            "   }" +
            "}"),

    RDF_COUNT_WORK_Q(Namespace.getNsPrefixString() +
            "SELECT (COUNT(DISTINCT ?uri) AS ?count) " +
            "WHERE {" +
            "   ?uri a bf:Work . " +
            "   FILTER (!isBlank(?uri))" +
            "   %1$s" +
//            "   FILTER NOT EXISTS{?instance bf:translationOf ?uri .}" +
            "}"),

    RDF_QUERY_WORK_INFO(Namespace.getNsPrefixString() +
            "SELECT ?creator ?label ?note " +
            "WHERE {" +
            "   <%1$s> bf:title/rdfs:label ?label ." +
            "   FILTER (!isBlank(?uri))" +
            "   OPTIONAL {<%1$s> bf:summary/rdfs:label ?note .}" +
            "   OPTIONAL {<%1$s> dc:creator ?creator .}" +
//          "   OPTIONAL {<%1$s> dc:creator ?creator .}" +
            "}"),

    RDF_QUERY_WORK_Q(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri " +
            "WHERE {" +
            "   ?uri a bf:Work. " +
//            "        bf:title/rdfs:label ?label . " +
            "   FILTER (!isBlank(?uri))" +
//            "   FILTER NOT EXISTS{?instance bf:translationOf ?uri .}" +
//            "   OPTIONAL {?uri rdfs:label ?eng . FILTER (lang(?eng) = 'eng')}" +
//            "   OPTIONAL {?uri bf:summary/rdfs:label ?note .}" +
//            "   OPTIONAL {?uri dc:creator ?creator .}" +
//            "   OPTIONAL {" +
//            "       SELECT ?uri (GROUP_CONCAT(DISTINCT ?genre; separator='; ') AS ?genres) " +
//            "       WHERE {" +
//            "           ?uri bf:genreForm/bf:identifiedBy/rdfs:label ?genre . " +
//            "           FILTER (lang(?genre) = 'zh-cn')" +
//            "       } GROUP BY ?uri" +
//            "   }" +
            "   %1$s" + 
            "   optional { ?uri bf:title ?bf_title. ?bf_title rdfs:label ?title } " + 
            "   optional { ?uri dc:creator ?creator } " + 
            "   } %2$s OFFSET %3$s LIMIT %4$s"),

    RDF_FACET_WORK_CS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?cnt (STR(?value) AS ?facet_text) " +
            "WHERE {" +
            "   ?vu rdfs:label ?value . " +
            "   FILTER (lang(?value) = 'zh-cn')" +
            "   {" +
            "       SELECT DISTINCT (count(DISTINCT ?uri) as ?cnt) ?vu {  " +
            "           ?uri a bf:Work .  " +
            "           ?uri %2$s ?vu . " +
            "           %1$s" +
            "       } GROUP BY ?vu" +
            "   }" +
            "} ORDER BY DESC(?cnt)"),

    RDF_FACET_WORK_LANGUAGE(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?cnt (STR(?value) AS ?facet_text) " +
            "WHERE {" +
            "   ?vu rdfs:label ?value . " +
            "   FILTER (lang(?value) = 'zh-cn')" +
            "   {" +
            "       SELECT DISTINCT (count(DISTINCT ?uri) as ?cnt) ?vu {  " +
            "           ?uri a bf:Work .  " +
            "           ?uri bf:language ?lang . " +
            "           ?lang bf:identifiedBy ?vu ; bf:part '正文语种'. " +
            "           %1$s" +
            "       } GROUP BY ?vu" +
            "   }" +
            "} ORDER BY DESC(?cnt)"),

    RDF_FACET_WORK_Q(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?cnt (STR(?value) AS ?facet_text) ?context " +
            "WHERE {" +
            "   ?vu rdfs:label ?value . " +
            "   FILTER (lang(?value) = 'zh-cn')" +
            "   {" +
            "       SELECT DISTINCT (count(DISTINCT ?uri) as ?cnt) ?vu ('audience' AS ?context) {  " +
            "           ?uri a bf:Work .  " +
            "           ?uri bf:intendedAudience/bf:identifiedBy ?vu . " +
            "           %1$s" +
            "       } GROUP BY ?vu ORDER BY DESC(?cnt) LIMIT 20" +
            "   } UNION {" +
            "       SELECT DISTINCT (count(DISTINCT ?uri) as ?cnt) ?vu ('genre' AS ?context) {  " +
            "           ?uri a bf:Work .  " +
            "           ?uri bf:genreForm/bf:identifiedBy ?vu . " +
            "           %1$s" +
            "       } GROUP BY ?vu ORDER BY DESC(?cnt) LIMIT 20" +
//            "           FILTER(BOUND(?value)) " +
//            "           FILTER (lang(?value) = 'zh-cn')" +
//            "           FILTER (!isBlank(?value)) " +
//            "           BIND(IF(ISURI(?value), REPLACE(STR(?value), \"^.+/(.+?)$\", \"$1\"), STR(?value)) AS ?facet_text) " +
            "   } UNION {" +
            "       SELECT DISTINCT (count(DISTINCT ?uri) as ?cnt) ?vu ('language' AS ?context) {  " +
            "           ?uri a bf:Work .  " +
            "           ?uri bf:language/bf:identifiedBy ?vu . " +
            "           %1$s" +
            "       } GROUP BY ?vu ORDER BY DESC(?cnt) LIMIT 20" +
            "   }" +
            "} "),

	RDF_QUERY_PO4S_LITERAL(Namespace.getNsPrefixString()
			+ "SELECT ?p ?o "
			+ "WHERE {" + "   <%1$s> ?p ?o . " + "   FILTER isLiteral(?o)"
			+ "}"),
	

	RDF_QUERY_PO4S_BLANK(Namespace.getNsPrefixString()
			+ "SELECT ?p ?o "
			+ "WHERE {" + "   <%1$s> ?p ?o . " + "   FILTER isBlank(?o)"
			+ "}"),
	
	RDF_QUERY_O4SP(Namespace.getNsPrefixString()
			+ "SELECT  ?o "
			+ "WHERE {" + "   <%1$s> %2$s ?o ."
			+ "}"),

    
	RDF_QUERY_OFFICIALEVENT_BY_PERSON(Namespace.getNsPrefixString()
			+ "SELECT ?eve ?relatedOrganization ?officialPosition (GROUP_CONCAT(DISTINCT ?begin ;separator=';') AS ?begin ) (GROUP_CONCAT(DISTINCT ?end;separator=';') AS ?end) ?identifier " + 
			" WHERE{<%1$s> shl:officialEvent ?eve. " + 
			" ?eve shl:relatedOrganization ?relatedOrganization. " + 
			" OPTIONAL{?eve shl:officialPosition/rdfs:label ?officialPosition}. " + 
			" OPTIONAL{?eve shl:temporal/shl:begin ?begin}. " + 
			" OPTIONAL{?eve shl:temporal/shl:end ?end}. " + 
			" OPTIONAL{?eve dc:identifier ?identifier} " + 
			"} order by asc(?begin) asc(?end) "),
    
	RDF_QUERY_TEMPORAL_BY_EVENT(Namespace.getNsPrefixString()
			+ "SELECT ?begin ?end " + 
			" WHERE{<%1$s> shl:temporal ?temp. " + 
			" OPTIONAL{?temp shl:begin ?begin}. " + 
			" OPTIONAL{?temp shl:end ?end}. " + 
			"} order by asc(?begin) asc(?end)"),
	
    RDF_COUNT_CPC_PERSONS(Namespace.getNsPrefixString() +
            "SELECT (COUNT(DISTINCT ?uri) AS ?count) " +
            "WHERE {" +
            "   ?uri a shl:Person. " +
            "   %1$s" +
            "}"),
	
    RDF_QUERY_CPC_PERSONS(Namespace.getNsPrefixString() +
            "SELECT DISTINCT ?uri WHERE { " +
    		"{ SELECT DISTINCT ?uri " +
            "WHERE { " +
            " ?uri a shl:Person. " +
            " ?uri foaf:name ?enName.  filter (lang(?enName) = 'en') " +
            "  %1$s " +
            " } %2$s } } OFFSET %3$s LIMIT %4$s "),
    ;

    protected String _query;

    RdfQuery(String query){
        this._query = query;
    }

    @Override
    public String toString() {
        return this._query;
    }

    /**
     * Query with formatting
     * @param args
     * @return
     */
    public String toString(Object ... args){
        return String.format(this._query, args);
    }
}
